package com.lzp.systemopt.camera.save

import android.media.Image
import com.lzp.systemopt.camera.CaptureListener
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

/**
 * @description:
 * @author: lxp
 * @date: 2022/1/24 下午1:43
 */
class ImageSaver(private val mImage: Image,private val mFile: File,private val mCaptureListener:CaptureListener? = null):Runnable  {
    override fun run() {
        val buffer = mImage.planes[0].buffer
        val bytes = ByteArray(buffer.remaining())
        buffer[bytes]
        var output: FileOutputStream? = null
        try {
            output = FileOutputStream(mFile)
            output.write(bytes)
            mCaptureListener?.onCaptureSuccess(mFile)
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            mImage.close()
            output?.let {
                try {
                    it.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }
}